package net.penguinmenac3.math;

/**
 * Gives some handy helper functions on arrays of MathObjects.
 * 
 * @author Michael
 */
public class Vector {
	private MathObject[] v;

	/**
	 * Create a new Vector.
	 * 
	 * @param v
	 */
	public Vector(MathObject[] v) {
		if (v == null) {
			throw new NullPointerException();
		}
		if (v.length == 0) {
			throw new IllegalArgumentException(
					"Vector must have length larger than 1");
		}
		this.v = v;
	}

	/**
	 * Checks if a vector is a nullvector.
	 * 
	 * @return
	 */
	public boolean isNull() {
		if (getFirstIndexOfNonNullElement() == Integer.MAX_VALUE) {
			return true;
		}
		return false;
	}

	/**
	 * Give the index of the first element that is not 0.
	 * 
	 * @return -1 for a NullVector.
	 */
	public int getFirstIndexOfNonNullElement() {
		for (int i = 0; i < v.length; i++) {
			if (!v[i].isNullElement()) {
				return i;
			}
		}
		return Integer.MAX_VALUE;
	}
	
	@Override
	public Vector clone() {
		MathObject[] out = new MathObject[v.length];
		for (int i = 0; i < v.length; i++) {
			out[i] = v[i].clone();
		}
		return new Vector(out);
	}

	/**
	 * Returns the array of the vector.
	 * 
	 * @return
	 */
	public MathObject[] toArray() {
		return v;
	}
}
